package com.ljkj.cordial.chat.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lanjiu.lib.utils.CollectionUtils;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.activity.MulPhotoPreviewActivity;
import com.ljkj.cordial.chat.bean.ChatTableUnit;
import com.ljkj.cordial.chat.bean.MsgType;
import com.ljkj.cordial.chat.bean.PayLoad;
import com.ljkj.cordial.chat.dao.ChatTableBean;
import com.ljkj.cordial.chat.presenter.AChatPresenter;
import com.ljkj.cordial.chat.presenter.ChatPresenterFactory;
import com.ljkj.cordial.chat.presenter.FilePresenter;
import com.ljkj.cordial.chat.presenter.OnChatViewItemClickListener;
import com.ljkj.cordial.chat.presenter.PicturePresenter;
import com.ljkj.cordial.chat.presenter.VoicePresenter;
import com.ljkj.cordial.chat.util.ReadFinishTask;
import com.nic.view.util.DateTimeUtil;
import com.nic.view.util.JLog;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements AChatPresenter.DataSetGetter {

    public final static int TYPE_RECEIVE_TEXT = 1;
    public final static int TYPE_RECEIVE_FILE = 2;
    public final static int TYPE_RECEIVE_PICTURE = 3;
    public final static int TYPE_RECEIVE_VIDEO = 4;
    public final static int TYPE_RECEIVE_VOICE = 5;
    public final static int TYPE_RECEIVE_EMOJI = 6;
    public final static int TYPE_RECEIVE_READ_FINISH = 7;
    public final static int TYPE_RECEIVE_HISTORY_TIME = 8;
    public final static int TYPE_SEND_TEXT = 9;
    public final static int TYPE_SEND_FILE = 10;
    public final static int TYPE_SEND_PICTURE = 11;
    public final static int TYPE_SEND_VIDEO = 12;
    public final static int TYPE_SEND_VOICE = 13;
    public final static int TYPE_SEND_EMOJI = 14;
    public final static int TYPE_SEND_READ_FINISH = 15;
    public final static int TYPE_SEND_HISTORY_TIME = 16;
    public final static int TYPE_RECEIVE_DEL_CHAT_MSG = 17;
    public final static int TYPE_SEND_DEL_CHAT_MSG = 18;
    private Context context;
    private int chatType;
    private List<ChatTableUnit> list = new ArrayList<>();
    ;
    private ReadFinishTask readFinishTask;
    private boolean isLoadAccess = true;
    private RecyclerView recycleView;

    public void setPause(boolean pause) {
        readFinishTask.setPause(pause);
    }

    public void setLoadAccess(boolean loadAccess) {
        isLoadAccess = loadAccess;
    }

    public boolean isLoadAccess() {
        return isLoadAccess;
    }

    public ChatAdapter(Context context, int chatType, RecyclerView recycleView) {
        this.context = context;
        this.chatType = chatType;
        this.recycleView = recycleView;
        readFinishTask = new ReadFinishTask(context).setOnReadFinishComplete(new ReadFinishTask.OnReadFinishComplete() {


            @Override
            public void onComplete(int position) {
                readFinishText(position);
            }
        }).setOnReadFinishUpdateListener(new ReadFinishTask.OnReadFinishUpdateListener() {


            @Override
            public void onUpdate(int position) {
                setPayLoadPos(position, PayLoad.DOWN_TIME);
            }
        });
//        setHasStableIds(true);
    }


    /**
     * 阅后即焚
     *
     * @param position
     */
    private void readFinishText(int position) {
//        try {
//            this.list.set(position, msg);
//            notifyItemChanged(position, PayLoad.create());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        remove(position);
    }

    public void clearTopHistory(List<Long> times) {
//        Iterator<ChatTableUnit> it = getList().iterator();
//        while (it.hasNext()) {
//            ChatTableUnit bean = it.next();
//            if (bean.getChatType() != MsgType.History_Time) {
//                return;
//            }
//            it.remove();
//        }
        for (ChatTableUnit tableUnit : getList()) {
            for (Long time : times) {
                if (tableUnit.getId().longValue() == time.longValue()) {
                    tableUnit.setChatType(MsgType.Del_Chat_Msg);
                }
            }
        }
    }

    /**
     * 设置消息成功状态
     *
     * @param msg
     * @param position
     */
    public void setErrorItem(ChatTableUnit msg, int position) {
        try {

            this.list.set(position, msg);
//            notifyItemInserted(position);
            notifyItemChanged(position, PayLoad.create(PayLoad.REQ_STATE));
//            refreshVisibleView(position);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置消息失败状态
     *
     * @param msg
     * @param position
     */
    public void setSuccessItem(ChatTableUnit msg, int position) {
        try {
            this.list.set(position, msg);
//            notifyItemInserted(position);
            notifyItemChanged(position, PayLoad.create(PayLoad.REQ_STATE));
//            refreshVisibleView(position);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void refreshVisibleView(int position) {
        if (refreshBottomListener != null) {
            refreshBottomListener.onBottom(position);
        }
    }

    /**
     * 插入消息已被删除状态
     *
     * @param msg
     * @param position
     */
    public void insertReceiveDelMsg(ChatTableUnit msg, int position) {
        try {
            msg.setChatType(MsgType.Del_Chat_Msg);
            this.list.add(position, msg);
            notifyItemInserted(position);
            //刷新下标，不然下标就不连续
            notifyItemRangeChanged(position, list.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getChatType() {
        return chatType;
    }

    public void setList(List<ChatTableUnit> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        this.list = list;
//        sort();
        notifyDataSetChanged();
        updateReadFinishData();
    }

    public void addListAll(List<ChatTableUnit> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
//        int position = this.list.size() - 1;
        if (!this.list.isEmpty()) {
            Iterator<ChatTableUnit> it = list.iterator();
            while (it.hasNext()) {
                ChatTableUnit c = it.next();
                for (ChatTableUnit bean : this.list) {
                    if (bean.getId() == c.getId()) {
                        it.remove();
                    }
                }
            }
        }

        this.list.addAll(0, list);
        notifyItemRangeInserted(0, list.size());
        updateReadFinishData();
    }

    public void add(ChatTableUnit bean) {
        this.list.add(bean);
        notifyItemInserted(list.size());
//        notifyItemChanged(list.size(), PayLoad.create());
        refreshVisibleView(list.size() - 1);
        updateReadFinishData();
    }


    public void remove(int position) {
        synchronized (list) {
            if (position >= 0 && position < getItemCount()) {
                ChatTableUnit c = this.list.get(position);
                ChatTableBean.deleteById(c.getId());
                this.list.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, getItemCount(), PayLoad.create());
//                refreshVisibleView(position);
                if (c.getRelateId() != null) {
                    remove(c.getRelateId());
                }
                updateReadFinishData();
            }
        }


    }

    public void remove(Long id) {
        if (id <= 0L || list.isEmpty()) {
            return;
        }
        synchronized (list) {
            Iterator<ChatTableUnit> it = list.iterator();
            int pos = 0;
            while (it.hasNext()) {
                ChatTableUnit chat = it.next();
                if (chat.getId().longValue() == id.longValue()) {
                    remove(pos);
                    return;
                }
                pos++;
            }
        }
    }


    /**
     * 刷新
     *
     * @param positionStart
     * @param itemCount
     */
    public void setPayLoad(int positionStart, int itemCount) {
        notifyItemRangeChanged(positionStart, itemCount, PayLoad.createNoAvatar());
    }

    public void setPayLoadAll() {
        notifyItemRangeChanged(0, getItemCount(), PayLoad.create());
    }

    public void setPayLoadAllText(int positionStart, int itemCount) {
        notifyItemRangeChanged(positionStart, list.size() > itemCount ? itemCount : itemCount - 1, PayLoad.create(PayLoad.TEXT));
    }


    public void setPayLoadPos(int position) {
        notifyItemChanged(position, PayLoad.create());
    }

    public void setPayLoadEnd(int position) {
        notifyItemChanged(position, PayLoad.createNoAvatar());
    }

    public void setPayLoadPos(int position, int... payType) {
        PayLoad payLoad = new PayLoad();
        payLoad.setPosition(position);
        payLoad.setItemCount(1);
        payLoad.setType(payType);
        notifyItemChanged(position, payLoad);
    }

    public List<ChatTableUnit> getList() {
        return list;
    }

    public List<ChatTableBean> getTableList() {
        List<ChatTableBean> cl = new ArrayList<>();
        for (ChatTableUnit unit : list) {
            cl.add(ChatTableUnit.getTable(unit));
        }
        return cl;
    }


    /**
     * 多选
     */
    public void selectAll() {
        if (!this.list.isEmpty()) {
            for (ChatTableUnit ChatTableUnit : this.list) {
                ChatTableUnit.setChecked(true);
            }
        }
//        notifyDataSetChanged();
        notifyItemRangeChanged(0, getItemCount(), PayLoad.create(PayLoad.SELECTED));
    }

    /**
     * 反选
     */
    public void unselectAll() {
        if (!this.list.isEmpty()) {
            for (ChatTableUnit ChatTableUnit : this.list) {
                ChatTableUnit.setChecked(false);
            }
        }
//        notifyDataSetChanged();
        notifyItemRangeChanged(0, getItemCount(), PayLoad.create(PayLoad.UNSELECT));
    }

    /**
     * 选中项
     *
     * @return
     */
    public List<ChatTableBean> selected() {
        List<ChatTableBean> list = new ArrayList<>();
        if (!this.list.isEmpty()) {
            for (ChatTableUnit cu : this.list) {
                if (cu.getChatType() != MsgType.History_Time && cu.isChecked()) {
                    list.add(ChatTableUnit.getTable(cu));
                }
            }
        }
        return list;
    }


    /**
     * 删除选中项
     */
    public void removeSelected() {
        List<Long> times = new ArrayList<>();
        for (Iterator<ChatTableUnit> it = this.list.iterator(); it.hasNext(); ) {
            ChatTableUnit cu = it.next();
            if (cu.isChecked()) {
                ChatTableBean.delete(ChatTableBean.getTableFromId(String.valueOf(cu.getId())));
                if (cu.getRelateId() != null) {
                    ChatTableBean.deleteById(cu.getRelateId());
                    times.add(cu.getRelateId());
                }
                it.remove();
            }
        }
//        clearTopHistory(times);
//        notifyDataSetChanged();
        for (ChatTableUnit tableUnit : getList()) {
            for (Long time : times) {
                if (tableUnit.getId().longValue() == time.longValue()) {
                    tableUnit.setChatType(MsgType.Del_Chat_Msg);
                }
            }
        }
        notifyItemRangeChanged(0, getItemCount(), PayLoad.create());
        updateReadFinishData();

    }

    public void removeIn(String ids) {
        List<Long> times = new ArrayList<>();
        for (Iterator<ChatTableUnit> it = this.list.iterator(); it.hasNext(); ) {
            ChatTableUnit c = it.next();
            if (ids.contains(String.valueOf(c.getId()))) {
                if (c.getRelateId() != null) {
                    ChatTableBean.deleteById(c.getRelateId());
                    times.add(c.getRelateId());
                }
                it.remove();
            }
        }
        for (ChatTableUnit tableUnit : getList()) {
            for (Long time : times) {
                if (tableUnit.getId().longValue() == time.longValue()) {
                    tableUnit.setChatType(MsgType.Del_Chat_Msg);
                }
            }
        }
//        clearTopHistory(times);
//        notifyDataSetChanged();
        notifyItemRangeChanged(0, getItemCount(), PayLoad.create());
        updateReadFinishData();
    }

    /**
     * 显示
     */
    public void selectVisible() {
        if (!this.list.isEmpty()) {
            for (ChatTableUnit ChatTableUnit : this.list) {
                ChatTableUnit.setVisible(true);
            }
        }
//        notifyDataSetChanged();
        notifyItemRangeChanged(0, getItemCount(), PayLoad.create(PayLoad.VISIBLE));
    }

    /**
     * 隐藏
     */
    public void selectInVisible() {
        if (!this.list.isEmpty()) {
            for (ChatTableUnit ChatTableUnit : this.list) {
                ChatTableUnit.setVisible(false);
            }
        }
//        notifyDataSetChanged();
        notifyItemRangeChanged(0, getItemCount(), PayLoad.create(PayLoad.INVISIBLE));
    }

    private OnChatViewItemClickListener onChatViewResultListener;

    public void setOnChatViewResultListener(OnChatViewItemClickListener onChatViewResultListener) {
        this.onChatViewResultListener = onChatViewResultListener;
    }

    private AChatPresenter.OnLongItemClickListener longItemClickListener;

    public void setLongItemClickListener(AChatPresenter.OnLongItemClickListener longItemClickListener) {
        this.longItemClickListener = longItemClickListener;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        ChatTableUnit item = list.get(position);
        if (item.getSendType() == ChatTableUnit.SEND) {
            if (item.getChatType() == MsgType.Text) {
                return TYPE_SEND_TEXT;
            } else if (item.getChatType() == MsgType.Video) {
                return TYPE_SEND_VIDEO;
            } else if (item.getChatType() == MsgType.Voice) {
                return TYPE_SEND_VOICE;
            } else if (item.getChatType() == MsgType.Picture) {
                return TYPE_SEND_PICTURE;
            } else if (item.getChatType() == MsgType.Expression) {
                return TYPE_SEND_EMOJI;
            } else if (item.getChatType() == MsgType.ANNEX) {
                return TYPE_SEND_FILE;
            } else if (item.getChatType() == MsgType.Read_Finish) {
                return TYPE_SEND_READ_FINISH;
            } else if (item.getChatType() == MsgType.History_Time) {
                return TYPE_SEND_HISTORY_TIME;
            } else if (item.getChatType() == MsgType.Del_Chat_Msg) {
                return TYPE_SEND_DEL_CHAT_MSG;
            }
        } else {
            if (item.getChatType() == MsgType.Text) {
                return TYPE_RECEIVE_TEXT;
            } else if (item.getChatType() == MsgType.Video) {
                return TYPE_RECEIVE_VIDEO;
            } else if (item.getChatType() == MsgType.Voice) {
                return TYPE_RECEIVE_VOICE;
            } else if (item.getChatType() == MsgType.Picture) {
                return TYPE_RECEIVE_PICTURE;
            } else if (item.getChatType() == MsgType.Expression) {
                return TYPE_RECEIVE_EMOJI;
            } else if (item.getChatType() == MsgType.ANNEX) {
                return TYPE_RECEIVE_FILE;
            } else if (item.getChatType() == MsgType.Read_Finish) {
                return TYPE_RECEIVE_READ_FINISH;
            } else if (item.getChatType() == MsgType.History_Time) {
                return TYPE_RECEIVE_HISTORY_TIME;
            } else if (item.getChatType() == MsgType.Del_Chat_Msg) {
                return TYPE_RECEIVE_DEL_CHAT_MSG;
            }
        }
        return TYPE_SEND_HISTORY_TIME;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int type) {
        if (type == TYPE_SEND_TEXT) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_chat_right_text, null, false);
            return ChatPresenterFactory.create(AChatPresenter.TEXT, AChatPresenter.RIGHT).setChatType(chatType).getViewHolder(view);
        } else if (type == TYPE_SEND_VIDEO) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_chat_right_video, null, false);
            return ChatPresenterFactory.create(AChatPresenter.VIDEO, AChatPresenter.RIGHT).setChatType(chatType).getViewHolder(view);
        } else if (type == TYPE_SEND_VOICE) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_chat_right_voice, null, false);
            return ChatPresenterFactory.create(AChatPresenter.VOICE, AChatPresenter.RIGHT).setChatType(chatType).setDataSetGetter(this).getViewHolder(view);
        } else if (type == TYPE_SEND_PICTURE || type == TYPE_SEND_EMOJI) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_chat_right_pic, null, false);
            return ChatPresenterFactory.create(AChatPresenter.PICTURE, AChatPresenter.RIGHT).setChatType(chatType).getViewHolder(view);
        } else if (type == TYPE_SEND_FILE) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_chat_right_file, null, false);
            return ChatPresenterFactory.create(AChatPresenter.FILE, AChatPresenter.RIGHT).setChatType(chatType).getViewHolder(view);
        } else if (type == TYPE_SEND_READ_FINISH || type == TYPE_SEND_DEL_CHAT_MSG) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_chat_right_read_finish, null, false);
            return ChatPresenterFactory.create(AChatPresenter.READ_FINISH, AChatPresenter.RIGHT).setChatType(chatType).getViewHolder(view);
        } else if (type == TYPE_RECEIVE_TEXT) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_chat_left_text, null, false);
            return ChatPresenterFactory.create(AChatPresenter.TEXT, AChatPresenter.LEFT).setChatType(chatType).getViewHolder(view);
        } else if (type == TYPE_RECEIVE_VIDEO) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_chat_left_video, null, false);
            return ChatPresenterFactory.create(AChatPresenter.VIDEO, AChatPresenter.LEFT).setChatType(chatType).getViewHolder(view);
        } else if (type == TYPE_RECEIVE_VOICE) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_chat_left_voice, null, false);
            return ChatPresenterFactory.create(AChatPresenter.VOICE, AChatPresenter.LEFT).setChatType(chatType).setDataSetGetter(this).getViewHolder(view);
        } else if (type == TYPE_RECEIVE_PICTURE || type == TYPE_RECEIVE_EMOJI) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_chat_left_pic, null, false);
            return ChatPresenterFactory.create(AChatPresenter.PICTURE, AChatPresenter.LEFT).setChatType(chatType).getViewHolder(view);
        } else if (type == TYPE_RECEIVE_FILE) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_chat_left_file, null, false);
            return ChatPresenterFactory.create(AChatPresenter.FILE, AChatPresenter.LEFT).setChatType(chatType).getViewHolder(view);
        } else if (type == TYPE_RECEIVE_READ_FINISH || type == TYPE_RECEIVE_DEL_CHAT_MSG) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_chat_left_read_finish, null, false);
            return ChatPresenterFactory.create(AChatPresenter.READ_FINISH, AChatPresenter.LEFT).setChatType(chatType).getViewHolder(view);
        } else {
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.height = viewGroup.getContext().getResources().getDimensionPixelOffset(R.dimen.dimen_24);
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_chat_history_time_text, null, false);
            view.setLayoutParams(params);
            return ChatPresenterFactory.create(AChatPresenter.HISTORY_TIME, AChatPresenter.RIGHT).setChatType(chatType).getViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        int type = getItemViewType(i);
        ChatTableUnit item = list.get(i);
        AChatPresenter factory = ChatPresenterFactory.get(getPresenterType(type));
        factory.setLoadAccess(isLoadAccess()).setDirection(getDirection(item.getSendType() == ChatTableUnit.SEND)).setOnChatViewResultListener(onChatViewResultListener).bindViewHolder(viewHolder, item, i);
        if (factory instanceof FilePresenter) {
            FilePresenter filePresenter = (FilePresenter) factory;
            filePresenter.setLoadFinishListener(() -> readFinishTask.setStartAnnexReadFinish(true));

        } else if (factory instanceof VoicePresenter) {
            VoicePresenter filePresenter = (VoicePresenter) factory;
            filePresenter.setOnVoiceListener(() -> {

            });

        } else if (factory instanceof PicturePresenter) {
            PicturePresenter filePresenter = (PicturePresenter) factory;
            filePresenter.setOnPictureListener(new PicturePresenter.OnPictureListener() {
                @Override
                public void onItemClick(String chatId) {
                    List<ChatTableBean> list = new ArrayList<>();
                    for (ChatTableUnit cu : getList()) {
                        ChatTableBean chat = new ChatTableBean();
                        if (cu.getChatType() == MsgType.Picture) {
                            chat.setChatType(MsgType.Picture);
                            chat.setId(cu.getId());
                            chat.setAlbumId(cu.getAlbumId());
                            chat.setThumbPath(cu.getThumbPath());
                            chat.setGroupId(cu.getGroupId());
                            chat.setGroupType(cu.getGroupType());
                            chat.setTargetId(cu.getTargetId());
                            chat.setTargetType(cu.getTargetType());
                            chat.setFileUrl(cu.getFileUrl());
                            list.add(chat);
                        }
                    }
                    MulPhotoPreviewActivity.jumpTo(viewHolder.itemView.getContext(), list, chatId);
                }
            });

        }
        factory.setLongItemClickListener(new AChatPresenter.OnLongItemClickListener() {
            @Override
            public void onLongClick(View v, ChatTableUnit item) {
                if (longItemClickListener != null && item.getSendType() == ChatTableUnit.RECEIVE) {
                    longItemClickListener.onLongClick(v, item);
                }
            }
        });

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        if (!isLoadAccess()) {
            return;
        }
        if (payloads != null && !payloads.isEmpty()) {
            PayLoad payLoad = (PayLoad) payloads.get(0);
            int type = getItemViewType(position);
            ChatTableUnit item = list.get(position);
            AChatPresenter factory = ChatPresenterFactory.get(getPresenterType(type));
            factory.setLoadAccess(isLoadAccess())
                    .setDirection(getDirection(item.getSendType() == ChatTableUnit.SEND))
                    .setOnChatViewResultListener(onChatViewResultListener)
                    .bindViewHolder(this, position, holder, item, payLoad);
            factory.setLongItemClickListener(new AChatPresenter.OnLongItemClickListener() {
                @Override
                public void onLongClick(View v, ChatTableUnit item) {
                    if (longItemClickListener != null && item.getSendType() == ChatTableUnit.RECEIVE) {
                        longItemClickListener.onLongClick(v, item);
                    }
                }
            });
            factory.setOnFileNoExistListener(new AChatPresenter.OnFileNoExistListener() {
                @Override
                public void onCall(ChatTableUnit item, int position) {
                    remove(position);
                }
            });
        } else {
            onBindViewHolder(holder, position);
        }
    }

    private int getPresenterType(int type) {
        if (type == TYPE_SEND_TEXT || type == TYPE_RECEIVE_TEXT) {
            return AChatPresenter.TEXT;
        } else if (type == TYPE_SEND_VIDEO || type == TYPE_RECEIVE_VIDEO) {
            return AChatPresenter.VIDEO;
        } else if (type == TYPE_SEND_VOICE || type == TYPE_RECEIVE_VOICE) {
            return AChatPresenter.VOICE;
        } else if (type == TYPE_SEND_PICTURE || type == TYPE_SEND_EMOJI
                || type == TYPE_RECEIVE_PICTURE || type == TYPE_RECEIVE_EMOJI) {
            return AChatPresenter.PICTURE;
        } else if (type == TYPE_SEND_FILE || type == TYPE_RECEIVE_FILE) {
            return AChatPresenter.FILE;
        } else if (type == TYPE_SEND_READ_FINISH || type == TYPE_RECEIVE_READ_FINISH
                || type == TYPE_RECEIVE_DEL_CHAT_MSG || type == TYPE_SEND_DEL_CHAT_MSG) {
            return AChatPresenter.READ_FINISH;
        } else {
            return AChatPresenter.HISTORY_TIME;
        }
    }

    private int getDirection(boolean isSend) {
        return isSend ? AChatPresenter.RIGHT : AChatPresenter.LEFT;
    }

    public void changeChatSendStateByMsgId(String msgId, boolean isSnapchat, boolean isSuccess) {
        if (CollectionUtils.isEmpty(list)) return;
        for (int i = 0; i < list.size(); i++) {
            ChatTableUnit unit = list.get(i);
            ChatTableBean cu = ChatTableUnit.getTable(unit);
            if (cu.getMsgId() != null && msgId.equals(cu.getMsgId())) {
                cu.setSendState(isSuccess ? cu.SEND_SUCCESS : ChatTableUnit.SEND_FAIL);
                cu.setStartReadFinish(isSnapchat);
                if (isSuccess) {
                    cu.setSendState(ChatTableUnit.SEND_SUCCESS);
                    cu.setMsgTime(System.currentTimeMillis());
                    ChatTableBean.updateInx(cu);
                    setSuccessItem(ChatTableUnit.copy(cu), i);
                } else {
                    cu.setStartReadFinish(false);
                    cu.setSendState(ChatTableUnit.SEND_FAIL);
                    cu.setMsgTime(System.currentTimeMillis());
                    ChatTableBean.updateInx(cu);
                    setErrorItem(ChatTableUnit.copy(cu), i);
                }
                updateReadFinishData();
                JLog.d("EN " + cu.getId() + "-" + DateTimeUtil.getCurrentDateHHMMSS());
                return;
            }
        }
    }

    public void updateReadFinishData() {
        readFinishTask.start(getList());
    }

    private OnRefreshBottomListener refreshBottomListener;

    public void setRefreshBottomListener(OnRefreshBottomListener refreshBottomListener) {
        this.refreshBottomListener = refreshBottomListener;
    }

    @Override
    public List<ChatTableUnit> getDataSet() {
        return this.list;
    }

    @Override
    public RecyclerView getRecyclerView() {
        return recycleView;
    }

    public interface OnRefreshBottomListener {
        void onBottom(int position);
    }
}
